package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.common.util.CheckUtil;
import com.zoo.diannaopeijian.domain.*;
import com.zoo.diannaopeijian.service.ShipmentItemService;
import com.zoo.diannaopeijian.service.ShipmentRecordService;
import com.zoo.diannaopeijian.service.ShipmentService;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出货服务实现类
 */
@Service
public class ShipmentServiceImpl implements ShipmentService {
    @Resource
    private ShipmentItemService shipmentItemService;
    @Resource
    private ShipmentRecordService shipmentRecordService;
    @Resource
    private StockService stockService;

    @Override
    @Transactional
    public boolean shipmentWares(Integer staffId, List<ShipmentItem> shipmentItemList) {
        if (CheckUtil.nullCheck(staffId, shipmentItemList)) {
            return false;
        }

        // 创建出货记录时间
        ShipmentRecord shipmentRecord = new ShipmentRecord();
        shipmentRecord.setStaffId(staffId);
        shipmentRecord.setShiptRecTime(new Date());

        boolean result = shipmentRecordService.insertShipmentRecord(shipmentRecord);

        if (StringUtils.isEmpty(shipmentRecord.getShiptRecId())) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        List<Stock> stocks = new ArrayList<>();
        // 为出货记录项插入出货记录ID，建立之间的关系，同时创建库存对象
        for (ShipmentItem item : shipmentItemList) {
            item.setShiptRecId(shipmentRecord.getShiptRecId());
            stocks.add(Stock.buildStock(item));
        }

        result = result && shipmentItemService.insertShipmentItems(shipmentItemList);

        // 修改库存
        result = result && stockService.reduceStocks(stocks);

        if (!result) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return result;
    }

    @Transactional
    @Override
    public boolean reShipmentWares(List<ShipmentItem> shipmentItemList, Integer shipmentRecordId) {
        if (CheckUtil.nullCheck(shipmentItemList, shipmentRecordId)) {
            return false;
        }

        boolean result = true;

        // 删除原本的出货项
        ShipmentItem shipmentItem = new ShipmentItem();
        shipmentItem.setShiptRecId(shipmentRecordId);
        List<ShipmentItem> itemsBeDelete = shipmentItemService.deleteShipmentItems(shipmentItem);

        // 修改库存
        // 构造要修改的库存对象
        List<Stock> stocks = new ArrayList<>();
        for (ShipmentItem item : itemsBeDelete) {
            stocks.add(Stock.buildStock(item));
        }
        result = result && stockService.increaseStocks(stocks);

        // 插入出货记录项
        result = result && shipmentItemService.insertShipmentItems(shipmentItemList);

        // 修改库存
        // 修改库存
        stocks.clear();
        for (ShipmentItem item : shipmentItemList) {
            stocks.add(Stock.buildStock(item));
        }
        result = result && stockService.reduceStocks(stocks);

        if (!result) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return result;
    }
}
